package logic

import (
	"context"
	"errors"
	"video/models/mysql"

	"video/videorpc/internal/svc"
	"video/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentDelLogic {
	return &CommentDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 评论删除
func (l *CommentDelLogic) CommentDel(in *videorpc.CommentDelRequest) (*videorpc.CommentDelResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.FirstUserByUserId(mysql.DB, in.UserId)
	if err != nil {
		return nil, err
	}
	if user.ID == 0 {
		return nil, errors.New("不是自己的评论,不允许删除评论")
	}
	var comment mysql.Comment
	err = comment.FindComment(mysql.DB, in.UserId, in.VideoId)
	if err != nil {
		return nil, err
	}
	if comment.ID == 0 {
		return nil, errors.New("评论不存在,无需删除")
	}

	err = comment.CommentDel(mysql.DB, in.UserId, in.VideoId)
	if err != nil {
		return nil, errors.New("删除失败")
	}
	return &videorpc.CommentDelResponse{
		CommentId: int64(comment.ID),
	}, nil
}
